package prj.carmap.mobile.activity;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.model.UserInfo;
import prj.carmap.mobile.process.AutoServiceProcess;
import prj.carmap.mobile.process.LiveServiceProcess;
import prj.carmap.mobile.process.SellerTypeProcess;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar.LayoutParams;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.miaowang.salad.activity.SaladMainActivity;

/**
 * 用户信息界面
 *
 */
public class ProfileActivity extends SherlockActivity {

	static final String TAG = "ProfileActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_profile);
		CarmapApplication.currentPage = "mine";
		CarmapApplication.addActivity(this);

		// Inflate the custom view
		View customNav = LayoutInflater.from(this).inflate(R.layout.nav_seller_title_view, null);
		// Attach to the action bar
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
		getSupportActionBar().setCustomView(customNav);
		customNav.setLayoutParams(lp);
		Button returnBtn = (Button) customNav.findViewById(R.id.return_prev);
		returnBtn.setOnClickListener(new Button.OnClickListener() {//创建监听
					public void onClick(View v) {
//						ProfileActivity.this.startActivity(new Intent(
//								ProfileActivity.this, MainActivity.class));
						ProfileActivity.this.startActivity(new Intent(ProfileActivity.this, MainActivity.class));
						((Activity) ProfileActivity.this).finish();
					}
				});
		((TextView) customNav.findViewById(R.id.nav_title)).setText(this.getString(R.string.profile));
		Button mapBtn = (Button) customNav.findViewById(R.id.return_map);
		mapBtn.setVisibility(View.GONE);
		TextView tvCard = (TextView) findViewById(R.id.txtCardNo);
		TextView tvPhone = (TextView) findViewById(R.id.txtPhone);
		//TextView tvName = (TextView) findViewById(R.id.txtName);
		TextView tvModels = (TextView) findViewById(R.id.txtModels);
		TextView tvLicense = (TextView) findViewById(R.id.txtLicense);
		UserInfo userInfo = CarmapApplication.getInstance().userInfo;
		if (userInfo != null) {
			tvCard.setText(this.getString(R.string.login_user) + ":" + userInfo.cardNo);
			//tvName.setText(this.getString(R.string.login_name) + ":" + userInfo.name);
			tvPhone.setText(this.getString(R.string.login_phone) + ":" + userInfo.phone);
			String models = userInfo.models.equals("null")?"":userInfo.models;
			String license = userInfo.license.equals("null")?"":userInfo.license;
			tvModels.setText(this.getString(R.string.login_models) + ":" + models);
			tvLicense.setText(this.getString(R.string.login_license) + ":" + license);
			Log.i("sss", models+"_"+license);
		}
		else
		{
			tvCard.setText(this.getString(R.string.login_user) + ":         ");
			//tvName.setText(this.getString(R.string.login_name) + ":         ");
			tvPhone.setText(this.getString(R.string.login_phone) + ":        ");
			tvModels.setText(this.getString(R.string.login_models) + ":        ");
			tvLicense.setText(this.getString(R.string.login_license) + ":        ");
		}

		Button btnLogin = (Button) findViewById(R.id.btnLogout);
		btnLogin.setOnClickListener(new Button.OnClickListener() {//创建监听
			public void onClick(View v) {
				CarmapApplication.getInstance().userInfo = null;
				ProfileActivity.this.startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
				ProfileActivity.this.finish();
			}

		});
		
		Button btnChangePassword = (Button) findViewById(R.id.btnChangePassword);
		btnChangePassword.setOnClickListener(new Button.OnClickListener() {//创建监听
			public void onClick(View v) {
				ProfileActivity.this.startActivity(new Intent(ProfileActivity.this, ChangePasswordActivity.class));
			}

		});
		
		initMenuItem();
	}

	@Override
	protected void onResume() {
		super.onResume();

		CarmapApplication.currentPage = "mine";
	}

	@SuppressWarnings("deprecation")
	private void initMenuItem() {
		if ("type".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_type);
			menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this.findViewById(R.id.item_type_image);
			itemImage.setImageResource(R.drawable.type_select);
			// 底部菜单文字
			TextView itemText = (TextView) this.findViewById(R.id.item_type_text);
			itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
		}
//		else if ("search".equals(CarmapApplication.currentPage)) {
//			// 底部菜单项
//			View menuItem = this.findViewById(R.id.menu_item_search);
//			menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.bottom_background));
//			// 底部菜单图标
//			ImageView itemImage = (ImageView) this.findViewById(R.id.item_search_image);
//			itemImage.setImageResource(R.drawable.search_select);
//			// 底部菜单文字
//			TextView itemText = (TextView) this.findViewById(R.id.item_search_text);
//			itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
//		}
		else if ("mine".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_mine);
			menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this.findViewById(R.id.item_mine_image);
			itemImage.setImageResource(R.drawable.mine_select);
			// 底部菜单文字
			TextView itemText = (TextView) this.findViewById(R.id.item_mine_text);
			itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
		}
		else if ("salad".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_salad);
			menuItem.setBackgroundDrawable(getApplication().getResources()
					.getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this
					.findViewById(R.id.item_salad_image);
			itemImage.setImageResource(R.drawable.mine_select);
			// 底部菜单文字
			TextView itemText = (TextView) this
					.findViewById(R.id.item_salad_text);
			itemText.setTextColor(getApplication().getResources().getColor(
					R.color.white));
		}
//		else if ("more".equals(CarmapApplication.currentPage)) {
//			// 底部菜单项
//			View menuItem = this.findViewById(R.id.menu_item_more);
//			menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.menu_color_base));
//			// 底部菜单图标
//			ImageView itemImage = (ImageView) this.findViewById(R.id.item_more_image);
//			itemImage.setImageResource(R.drawable.more_select);
//			// 底部菜单文字
//			TextView itemText = (TextView) this.findViewById(R.id.item_more_text);
//			itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
//		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		View view = (View) this.findViewById(R.id.menu_item_home);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AutoServiceProcess process = new AutoServiceProcess();
				process.setContext(ProfileActivity.this);
				process.getSellerTypes();
			}
		});
		
		 view = (View) this.findViewById(R.id.menu_item_type);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LiveServiceProcess process = new LiveServiceProcess();
				process.setContext(ProfileActivity.this);
				process.getSellerTypes();
				ProfileActivity.this.finish();
			}
		});
		
		View saladView = (View) this.findViewById(R.id.menu_item_salad);
		saladView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ProfileActivity.this, SaladMainActivity.class);
//				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				ProfileActivity.this.startActivity(intent);
				ProfileActivity.this.finish();
			}
			
		});


//		view = (View) this.findViewById(R.id.menu_item_more);
//		view.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				ProfileActivity.this.startActivity(new Intent(ProfileActivity.this, MoreActivity.class));
//			}
//		});

//		view = (View) this.findViewById(R.id.menu_item_search);
//		view.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				CarmapApplication.popView = new DialogView(ProfileActivity.this);
//				CarmapApplication.popView.show();
//				ProfileActivity.this.startActivity(new Intent(ProfileActivity.this, SellerSearchActivity.class));
//				ProfileActivity.this.finish();
//			}
//		});

		return super.onCreateOptionsMenu(menu);
	}
}
